package com.wutong.erb.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wutong.erb.entity.user.Teacher;
import com.wutong.erb.entity.user.User;
import org.apache.ibatis.annotations.*;

/**
 * 用户Mapper
 * @author wutong
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("SELECT * FROM user WHERE username = #{username}")
    User selectByUsername(@Param("username") String username);

    @Select("SELECT COUNT(1) FROM user WHERE username = #{username}")
    boolean existsUsername(@Param("username") String username);

    @Select("SELECT * FROM user WHERE id = #{id}")
    User selectById(@Param("id") Long id);

    @Select("SELECT COUNT(1) FROM user WHERE id = #{id}")
    boolean existsId(@Param("id") Long id);

    @Select("SELECT * FROM user")
    Page<User> selectAllUser(Page<User> page);

    @Insert("INSERT INTO teacher_profile (user_id, organization, title, hire_date) VALUES (#{userId}, #{organization}, #{title}, #{hireDate})")
    int insertTeacherProfile(Teacher teacher);

    @Select("SELECT  * FROM user WHERE email = #{email}")
    User selectByEmail(@Param("email") String email);

    @Update("UPDATE user SET nickname = #{nickname}, email = #{email}, phone = #{phone}, sex = #{sex} WHERE id = #{id}")
    int updateUser(User user);

    @Update("UPDATE user SET password_hash = #{passwordHash} WHERE id = #{id}")
    int updatePassword(User user);
}
